package com.nsqt.biz.controller;

import com.nsqt.biz.domain.KeywordsDO;
import com.nsqt.biz.service.KeywordsService;
import com.nsqt.common.utils.PageUtils;
import com.nsqt.common.utils.Query;
import com.nsqt.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
@RequestMapping("/biz/keywords")
public class KeywordsController {
    @Autowired
    private KeywordsService keywordsService;

    @GetMapping()
    @RequiresPermissions("biz:keywords:keywords")
    String Keywords() {
        return "biz/keywords/keywords";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("biz:keywords:keywords")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<KeywordsDO> keywordsList = keywordsService.list(query);
        int total = keywordsService.count(query);
        PageUtils pageUtils = new PageUtils(keywordsList, total);
        return pageUtils;
    }

    /**
     * 新增
     */
    @GetMapping("/add")
    @RequiresPermissions("biz:keywords:add")
    String add() {
        return "biz/keywords/add";
    }

    /**
     * 编辑
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("biz:keywords:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        KeywordsDO keywords = keywordsService.get(id);
        model.addAttribute("keywords", keywords);
        return "biz/keywords/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("biz:keywords:add")
    public R save(KeywordsDO keywords) {
        if (keywordsService.save(keywords) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("biz:keywords:edit")
    public R update(KeywordsDO keywords) {
        keywordsService.update(keywords);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz:keywords:remove")
    public R remove(Integer id) {
        if (keywordsService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("biz:keywords:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        keywordsService.batchRemove(ids);
        return R.ok();
    }
}
